package com.dream.technology.controller;


import com.dream.technology.data.GenericRspDTO;
import com.dream.technology.data.NoBody;
import com.dream.technology.db.entity.RcCustomerTag;
import com.dream.technology.db.entity.RcCustomerTagItem;
import com.dream.technology.db.entity.RcCustomerTagQuery;
import com.dream.technology.db.entity.RcFlow;
import com.dream.technology.db.entity.vo.VoCustomerTag;
import com.dream.technology.service.RcCustomerTagItemService;
import com.dream.technology.service.RcCustomerTagService;
import com.dream.technology.service.RcFlowService;
import com.dream.technology.vo.ErrorCode;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/risk/admin/customer_tag")
public class RcCustomerTagController {

    private static final Logger logger = LoggerFactory.getLogger(RcCustomerTagController.class);

    @Autowired
    private RcCustomerTagService rcCustomerTagService;
    @Autowired
    private RcCustomerTagItemService rcCustomerTagItemService;
    @Autowired
    private RcFlowService rcFlowService;

    /**
     * 用户标签列表接口
     *
     * @param pageNum
     * @param pageSize
     * @param tagName
     * @param
     * @return
     */
    @GetMapping("/list")
    public GenericRspDTO<Object> getList(Integer pageNum, Integer pageSize, String tagName, Long customerTagTypeId) {
        RcCustomerTagQuery query = new RcCustomerTagQuery();
        RcCustomerTagQuery.Criteria criteria1 = query.createCriteria();
        RcCustomerTagQuery.Criteria criteria2 = query.createCriteria();
        criteria1.andStatusEqualTo(false);
        if (customerTagTypeId != null){
            criteria1.andTagTypeIdEqualTo(customerTagTypeId.intValue());
       }
        if (StringUtils.isNotBlank(tagName)){
             criteria1.andTagNameLike("%" + tagName + "%");
             criteria2.andCodeLike("%" + tagName + "%");
        }
        query.or(criteria2);
        query.setOrderByClause("enabled desc,create_date desc");
        PageHelper.startPage(pageNum, pageSize);
        List<RcCustomerTag> rcFieldSelectList = rcCustomerTagService.selectByExample(query);
        PageInfo<RcCustomerTag> pageInfo = new PageInfo<RcCustomerTag>(rcFieldSelectList);
        return GenericRspDTO.newInstance(ErrorCode.SUCCESS.getErrorCode(), ErrorCode.SUCCESS.getErrorMsg(), pageInfo);
    }

    /**
     * 添加用户标签
     *
     * @param customerTag
     *
     * @return
     */
    @PostMapping("/add")
    public GenericRspDTO<Object> addRcCustomerTag(@RequestBody VoCustomerTag customerTag) {
       logger.info("customerTag==={}",customerTag);

        String result = rcCustomerTagService.insertTag(customerTag);

        return GenericRspDTO.newInstance(ErrorCode.SUCCESS.getErrorCode(), ErrorCode.SUCCESS.getErrorMsg(), result);
    }

    /**
     * 获取用户标签回显接口
     *
     * @param id
     * @return
     */
    @GetMapping("/id")
    public GenericRspDTO<VoCustomerTag> getRcCustomerTagById(Long id) {

        RcCustomerTag rcCustomerTag = rcCustomerTagService.selectByPrimaryKey(id);
        List<RcCustomerTagItem> rcCustomerTagItems = rcCustomerTagItemService.selectByTagId(id);

        VoCustomerTag voCustomerTag = new VoCustomerTag();
        voCustomerTag.setCustomerTag(rcCustomerTag);
        voCustomerTag.setCustomerTagItems(rcCustomerTagItems);

        return GenericRspDTO.newInstance(ErrorCode.SUCCESS.getErrorCode(), ErrorCode.SUCCESS.getErrorMsg(), voCustomerTag);
    }


    /**
     * 修改用户标签接口
     *
     * @param rcCustomerTag
     * @return
     */
    @PostMapping("/update")
    public GenericRspDTO<Object> updateRcCustomerTag(@RequestBody VoCustomerTag rcCustomerTag) {

        rcCustomerTagService.updateByJsonStr(rcCustomerTag);

        return GenericRspDTO.newInstance(ErrorCode.SUCCESS.getErrorCode(), ErrorCode.SUCCESS.getErrorMsg(), null);
    }


	/**
	 * 删除或停用用户标签接口 (逻辑删除)
	 * @param id	用户标签ID
	 * @param status	是否删除：1删除，0不删除
	 * @param enabled	是否启用：1启用，0不启用
	 * @return
	 */
	@PostMapping("/delete")
	public GenericRspDTO<NoBody> deleteRcCustomerTag(Long id, Boolean status, Boolean enabled) {
		RcCustomerTag tag = new RcCustomerTag();
		tag.setId(id);
		if (status != null) {
			tag.setStatus(status);
		}
		if (enabled != null) {
			tag.setEnabled(enabled);
		}
		rcCustomerTagService.updateByPrimaryKeySelective(tag);

		return GenericRspDTO.newInstance(ErrorCode.SUCCESS.getErrorCode(), ErrorCode.SUCCESS.getErrorMsg(), null);
	}

    /**
     * 风控用户标签验证
     *
     * @param
     * @return
     */
    @PostMapping("/checkForDoc")
    public GenericRspDTO<Object> checkRcCustomerTag(@RequestBody VoCustomerTag customerTag) {
        String result = rcCustomerTagService.checkCustomerTag(customerTag);
        return GenericRspDTO.newInstance(ErrorCode.SUCCESS.getErrorCode(), ErrorCode.SUCCESS.getErrorMsg(), result);
    }

    /**
     * 风控用户标签修改前检查 返回引用该标签的流程集合
     *
     * @param
     * @return
     */
    @PostMapping("/checkRef")
    public GenericRspDTO<Map<String, Object>> updateBeforeCheck(Long customerTag) {
    	Map<String, Object> map = new HashMap<String, Object>();
        List<RcFlow> result = rcFlowService.queryFlowByTag(0, customerTag);
        map.put("list", result);
        return GenericRspDTO.newInstance(ErrorCode.SUCCESS.getErrorCode(), ErrorCode.SUCCESS.getErrorMsg(), map);
    }


    /**
     *  用户标签复制
     * @param id
     * @param name
     * @param  code
     * @return
     */
    @PostMapping("/copy")
    public GenericRspDTO<Object> copy(Long id, String tagName, String description, Boolean enabled){
        rcCustomerTagService.copy(id, tagName, description, enabled);

        return GenericRspDTO.newInstance(ErrorCode.SUCCESS.getErrorCode(), ErrorCode.SUCCESS.getErrorMsg(), "");
    }

    /**
     * 验证名称是否重复
     * @param name
     * @return
     */
    @GetMapping("/getByName")
    public GenericRspDTO<Object> getByName(String name){
    	RcCustomerTagQuery query = new RcCustomerTagQuery();
    	RcCustomerTagQuery.Criteria criteria = query.createCriteria();
    	criteria.andTagNameEqualTo(name);
    	int count = rcCustomerTagService.selectByExample(query).size();
    	return GenericRspDTO.newInstance(ErrorCode.SUCCESS.getErrorCode(), ErrorCode.SUCCESS.getErrorMsg(), count);
    }
    
}

